package net.lzzy.controller;

import net.lzzy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/login")
    public String login(){
        return "user/login";
    }

    @GetMapping("/logout")
    public String logout(){
        return "user/logout";
    }

    @GetMapping("/register")
    public String register(){
        return "user/register";
    }

    @PostMapping("/register")
    public String registerUser(@RequestParam("email") String email, @RequestParam("username") String username, @RequestParam("password") String password, Model model) {
        if (userService.register(email, username, password)){
            return "redirect:/login";
        }
        else {
            model.addAttribute("msg","该用户已注册！");
            return "user/register";
        }
    }
}
